package com.ce.util.chart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.ce.framework.util.StringUtil;

public class ChartGroup {

	private String title;
	private String color;
	private List<ChartData> data = new ArrayList<ChartData>();
	private HashMap<String,String> otherAttr = new HashMap<String,String>();
	
	public ChartGroup(String title) {
		super();
		this.title = title;
	}

	public ChartGroup(String title, String color) {
		super();
		this.title = title;
		this.color = color;
	}

	public ChartGroup addAttribute(String name,String value){
		otherAttr.put(name, value);
		return this;
	}
	
	public ChartData addData(String label,String value){
		return this.addData(new ChartData(label,value));
	}

	public ChartData addData(ChartData item){
		data.add(item);
		return item;
	}

	public Element toElement(){
		Element e = new Element("graph");
		Iterator<String> it = otherAttr.keySet().iterator();
		String key,val;
		while(it.hasNext()){
			key = it.next();val = otherAttr.get(key);
			e.setAttribute(key, val);
		}
		e.setAttribute("title", title);
		if(StringUtil.isNotBlank(color))
			e.setAttribute("color", color);
//		for(ChartData c:data){
//			e.addChild("value");
//		}
		return e;
	}

	public String toString(){
		return this.toElement().toString();
	}

	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof ChartGroup))
			return false;
		return this.getTitle().equals(((ChartGroup)obj).getTitle());
	}

	public String getTitle() {
		return title==null?"":title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public List<ChartData> getData() {
		return data;
	}

	public HashMap<String, String> getOtherAttr() {
		return otherAttr;
	}

}
